#ifndef _DB_HXX_
#define _DB_HXX_

#include <mytypes.h>
#include <mydll.h>

#if defined(WIN32)

#ifdef EXPORT
#undef EXPORT
#endif

#ifdef EXPORT_MYDB
/* DLL export */
#define EXPORT __declspec(dllexport)
#else
/* EXE import */
#define EXPORT __declspec(dllimport)
#endif

#else

#define EXPORT 

#endif

class DBImpl;
class FileLog;

class EXPORT DB 
{
public:
  typedef tSInt32  tQueryId;
  static const tSInt32 Undefined;
  static const tSInt32 SuccessButNoResult;

  DB(const string &server_address,
     const string &login, 
     const string &password, 
     const string &database, 
     tUInt16 port,FileLog *filelog=0);
  virtual ~DB();

  /// \brief Connect to the database with the information provided by SetConnectionInfo.
  ///
  bool Connect();

  /// \brief Disconnect from given database and free all related ressources.
  ///
  tBool Disconnect();

  /// \brief Returns the error code of the latest executed MYSQL statement.
  ///
  tUInt32 ErrNo() const;

  /// \brief Returns the last error message related to database operation.
  /// 
  const char *LastErrorMsg();

  /// \brief Returns the last error code related to database operation.
  /// 
  tSInt32 LastErrorCode();

  /// \brief Execute the given query and returns the associated query id.
  /// 
  /// In case of error, DB::Undefined is returned.
  tQueryId Query(const string &query);

  /// \brief Returns one line of the given executed query.
  /// 
  /// This function should be used after the use of Query
  /// function to get the result line by line. 
  /// If no query available anymore for that queryid,
  /// then NULL is returned.
  char **FetchQuery(tQueryId qid);

  /// \brief Returns the number of column of a given query result.
  ///
  tUInt32 NumFieldsQuery(tQueryId qid);

  /// \brief Free all ressources related to a given query.
  ///
  /// This function should be called for each Query function call
  /// in order to avoid memory leak.
  bool FreeQuery(tQueryId qid);

  //////////////////////////////////////////////////////////////
  tUInt32 LastInsertId();
  tUInt32 AffectedRows();


  void Ping();
private:
  // Copy constructor is private to FORCE the user to instantiate 
  // a new object with 'new' instead of static allocation.
  // This is because we cannot copy this object so easily 
  // mainly because of internal objects.
  DB(const DB &model);
 
  DBImpl *_impl;
};


#endif // _DB_HXX_
